#ifndef SCREEN_H
#define SCREEN_H

#define Screen_Password                         123

#define Screen_Recv_Buf_Max                     128
#define Screen_Source_Buf_Max                   36
#define Screen_Send_Buf_Max                     56

#define Screen_Zhongxian                        0
#define Screen_Diwen                            1

#define Screen_Water_Level_X                    611
#define Screen_Water_Level_Y_Min                140
#define Screen_Water_Level_Y_Max                353

#define Login_Page                              0x01
#define Monitoring_Page                         0x02            // 监控界面
#define Auto_Page                               0x03
//#define Set_Page                              0x03
#define Hpr_Prm_Page                            0x0a
#define Error_Page                              0x08



#define Screen_Send_Data_Len                    14
#define Screen_Send_Hyr_Prm_Data_Len            36
#define Screen_Send_Gate_Opening_Degree_Len     4

#define Gate_x                                  0
#define Gate_y                                  1
#define Gate_X_Pos                              611
#define Gate_Y_Low_Pos                          354.0f
#define Gate_Y_High_Pos                         254.0f

/*****上发数据时需要加入的参数**********************************/
/*******************item***************************************/
#define COORDINATE                              1               //闸门坐标数据
#define LOGIN_ITEM                              2               //登陆项
#define COLOR_ITEM                              3               //修改颜色项
#define SERVER_IP_ITEM                          4               //server ip
#define CUMULATIVE_FLOW                         5               //累计流量是第5项
#define PARAMETER_CONF_ITEM                     8               //阈值等信息
#define CONT_DATA_ITEM                          9               //串口屏可以配置给单片机的数据
#define REAL_TIME_DATA_ITEM                     10               //实时数据有13项,但是累计流量是4字节的，所以拆分成14项目
#define Hyper_Parameter_Item                    11               //超参数值
/*******************status***************************************/
#define LOGIN_OK                                0
#define LOGIN_ERROR                             1
#define IGNORE                                  2

/*******串口屏的地址参数*****************************/
#define REAL_DATA_START_POSITION                0x0000          //实时数据的起始地址

#define PARAMETER_START_POSITION                0x0010          //参数的起始地址
#define SERVER_IP_START_POSITION                0x0020          //server ip的起始地址
#define COORDINATE_START_POSITION               0x0104          //闸门坐标的起始地址
#define LOGIN_DATA_START_POSITION               0x0B00          //登陆数据的起始地址
#define COLOR_DATA_STAR_POSITION                0x4003          //颜色数据的起始地址

#define SCREEN_ADDRESS                          0x0103          //界面切换存储地址
#define Addr_Set_Gate_Up                        0x0100          //闸门上升按钮存储地址
#define Addr_Set_Gate_Down                      0x0101          //闸门下降按钮存储地址
#define Addr_Set_Gate_Stop                      0x0102          //闸门停止按钮存储地址

#define Addr_Hyper_Parameter_Data               0x1000          //超参数的起始地址
#define Addr_Study                              0x1111          //学习

#define VERTICAL_MOTION_ADDRESS                 0x0018          //闸门垂直运动地址

#define TRAFFIC_CONTROL_SET_ADDRESS             0x0019          //自动控制方式设置
#define Addr_Auto_Control                       0x0110          //自动控制开启

#define Addr_Constant_Flowmeter                 0x001A          //恒定流量
#define Addr_Constant_Gate_Opening              0x001B          //闸门开度
#define Addr_Constant_Water_Level               0x001C          //恒定水位

#define WATER_ALL_LIMIT_ADDRESS                 0x001D          //限量控制，立方米

#define CONSTANT_WATER_SPEED_ADDRESS            0x001E          //恒流量限时控制，设置流量地址
#define LIMIT_TIME_ADDRESS_2                    0x001F          //恒流量限时控制，限时地址

#define Addr_Password                           0x2020
#define Addr_Login                              0x2030

#define Addr_Reset                              0x0202

#define Addr_Auto_Page                          0x000F
#define Addr_Set_Screen_Close                   0x0200

#define Addr_Set_Hyper_Paremeter                0x0010
#define Addr_Get_Hyper_Paremeter                0x0011
/*****************************************************************/
#define LOGIN_PASSWD                            123
#define SEND_SIZE                               REAL_TIME_DATA_ITE
#define DATA_START                              6 //协议中数据真正开始的位置
/*****************************************************************/

typedef struct
{
  unsigned char  select;        //
  unsigned short password;
  unsigned short recv_len;
  unsigned short send_len;
  unsigned short send_xy_len;
  unsigned short recv_value;
  unsigned short address;
  unsigned char  recv_buf[Screen_Recv_Buf_Max];
  unsigned char  send_buf[Screen_Send_Buf_Max];
  unsigned char  send_xy_buf[Screen_Send_Buf_Max];
  unsigned short source_buf[Screen_Source_Buf_Max];
  unsigned short xy_value[2];
  float register_value[13];
  unsigned char  interface_option;
}__attribute__((packed, aligned(1))) SCREEN;

void screen_init_success( void );
void switch_Page( uint8_t Page );
uint16_t screen_data_protocol(uint16_t *source, uint8_t *target, int item, uint16_t size, int status);
uint16_t login_protocol(uint8_t *target, int status);
uint16_t color_protocol(uint8_t *target, int status);
uint16_t server_ip_protocol(uint16_t *source, uint8_t *target);
uint16_t waterlock_axis_protocol(uint16_t *source, uint8_t *target);
void screen_data_into_arry(uint8_t *target, int index, uint16_t data);
void screen_data_protocol_common_part(uint8_t *target, uint16_t start_position);
void response_operation(uint16_t address, uint16_t value);
ErrorStatus screen_data_analysis( SCREEN *screen );
void recv_screen_package_callback( unsigned char *data, unsigned short size );
void recv_screen_package_handle( void );
void screen_send_data( uint16_t *data, uint8_t item, uint16_t size );
#endif